use actix_web::{middleware::from_fn, App, HttpServer};
use land_dev::{
    config::{app::AppConfig, auth::AuthPath, doc::ApiDoc, logging::Logging, state::AppState},
    middlewares::{auth_middleware::Authentication, logging_middleware::http_logger},
    routes::api_routes::ApiRoutes,
};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let config = AppConfig::init();

    Logging::init(&config).await;

    let state = AppState::init(&config).await;

    AuthPath::init(&state).await.unwrap();

    HttpServer::new(move || {
        App::new()
            .service(
                SwaggerUi::new("/swagger-ui/{_:.*}")
                    .url("/api-docs/openapi.json", ApiDoc::openapi()),
            )
            .wrap(Authentication)
            .configure(ApiRoutes::init)
            .app_data(state.clone())
            .wrap(from_fn(http_logger))
    })
    .bind((config.server_addr, config.server_port))?
    .run()
    .await
}
